<?php

namespace App\Http\Controllers;

use App\Models\Sms;
use Cloud;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input as Input;

/**
 * 通用方法控制器
 */
class PublicController extends Controller
{
    /**
     *  发送验证码
     *  1-登录 2-修改密码 3-注册 4-绑定银行卡 6-修改支付密码 7-更换手机号.
     */
    public function send_code(Request $request)
    {

        try {
            $status  = 0;
            $phone   = $request->get('phone');
            $type    = $request->get('type');
            $expired = $request->get('expired') ?? 80;

            if (empty($phone) || empty($type)) {
                throw new Exception('缺少关键参数！');
            }
            if (!isPhone($phone)) {
                throw new Exception('手机号码错误！');
            }

            /*$code     = getCode(); //获取短信验证码
            $template = getTemplate($type); //根据类型获取短信模板
            $result   = Cloud::sendTemplateSMS($phone, [$code, $expired . '秒'], $template); //发送短信验证码
            //发送失败
            if (!$result['resultStatus']) {
                throw new Exception('验证码发送失败！');
            } else {
                //存入数据库
                $sms  = Sms::where(['phone' => $phone, 'type' => $type])->first();
                $time = time();
                if ($sms) {
                    $sms->update([
                        'code'       => $code,
                        'send_at'    => $time,
                        'expired_at' => $time + $expired,
                    ]);
                } else {
                    Sms::create([
                        'phone'      => $phone,
                        'code'       => $code,
                        'type'       => $type,
                        'send_at'    => $time,
                        'expired_at' => $time + $expired,
                    ]);
                }
            }*/
            $status = 1;
            $msg    = "发送成功！";
        } catch (Exception $e) {
            $msg = $e->getMessage();
        }

        return response()->json(['status' => $status, 'msg' => $msg]);
    }
}
